package com.hung.triple.transport.netty4.handler;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 完成活跃 channel 的注册
 *
 * @author Hung
 * @since 2022/9/18
 */
@Slf4j
@AllArgsConstructor
@Component
public class NettyChannelDuplexHandler extends ChannelDuplexHandler {

    private final boolean isClient;

    /**
     * channel 活跃的回调
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }

    /**
     * channel 不活跃的回调
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

//        String address = AddressUtils.getAddress(ctx.channel());
//
//        // 删除传输器
//        TransporterPool.removeTransporter(address);
//        // 重新建立 provider 连接
//        if (isClient) {
//            log.info("[Triple] channel of remote({}) is inactive, try to reconnect to available provider", address);
//            NettyConnectionFactory.loseProviders(address);
//        }


        super.channelInactive(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("[Triple] netty channel exceptionCaught", cause);
    }
}
